package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService ;//合同service

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    /**
     * 分页查询合同状态=1的合同
     * 请求连接contractList
     */
    @RequestMapping("/contractList")
    public String contractList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {

        //创建example对象
        ContractExample example = new ContractExample();
        //通过example创建criteria对象
        ContractExample.Criteria criteria = example.createCriteria();
        //向criteria对象设置条件   where company_id=?
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        //设置状态=1
        criteria.andStateEqualTo(1);
        //设置排序 ORDER BY create_time DESC
        example.setOrderByClause("create_time DESC");
        PageInfo info = contractService.findAll(page, size, example);
        request.setAttribute("page", info);
        return "cargo/export/export-contractList";
    }

    /**
     * 分页查询
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {

        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo info = exportService.findAll(example, page, size);

        request.setAttribute("page", info);
        return "cargo/export/export-list";
    }

    /**
     * 进入保存页面
     *  请求连接：toExport
     *  参数：同名参数id
     *          * 使用springmvc，转化为id数组
     *          * 使用web规范，转化为id字符串 （多个之间“，”隔开）
     */
    @RequestMapping("/toExport")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "cargo/export/export-toExport";
    }

    /**
     * 进入到修改页面
     *   请求连接： toUpdate
     *   参数：id（报运单id）
     *   业务：
     *    1、根据id查询报运单
     *    2、根据报运单id，查询报运单的所有商品（不要分页）
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //1、根据id查询报运单
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        //2、根据报运单id，查询报运单的所有商品（不要分页)
        ExportProductExample example = new ExportProductExample();
        //根据所属的报运单id查询 （ExportProduct其中有一个属性：exportId）
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(example);
        request.setAttribute("eps",list);
        return "cargo/export/export-update";
    }

    /**
     * 新增或者修改
     *   请求连接 edit
     *   参数：报运单对象（Export）
     */
    @RequestMapping("/edit")
    public String edit(Export export) {
        export.setCompanyId(getLoginCompanyId());
        export.setCompanyName(getLoginCompanyName());
        if(StringUtils.isEmpty(export.getId())) {
            exportService.save(export);
        }else{
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }


    /**
     * 电子报运：将报运单数据发送到海关平台
     * 请求连接：exportE.do?id=3653cf35-da5a-4e85-b1dd-10461ba19152
     * 参数：报运单id
     */
    @RequestMapping("/exportE")
    public String exportE(String id) {
        exportService.exportE(id); //在service中完成保运业务
        return "redirect:/cargo/export/list.do";
    }
}
